package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.model.dto.CourseCategoryDto;
import com.xuecheng.content.model.mapper.CourseMapperStruct;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.service.CourseCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author luhao
 */
@Slf4j
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {
    @Autowired
    private CourseMapperStruct courseMapperStruct;

    @Override
    public List<CourseCategoryDto> queryByNodes(String id) {
        // 查询出课程分类
        List<CourseCategory> courseCategories = this.baseMapper.selectTreeNodes(id);
       /* TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("id");
        config.setNameKey("name");
        config.setParentIdKey("parentid");
        config.setWeightKey("orderby");
        config.setChildrenKey("childrenTreeNodes");
        //转换成树形结构
        List<Tree<String>> treeNodes = TreeUtil.build(courseCategories, id, config,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentid());
                    tree.setWeight(treeNode.getOrderby());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("label", treeNode.getLabel());
                    tree.putExtra("isShow", treeNode.getIsShow());
                    tree.putExtra("isLeaf", treeNode.getIsLeaf());
                });
        String jsonStr = JSONUtil.toJsonStr(treeNodes);
        System.out.println(jsonStr);*/
        // Po -> Dto
        List<CourseCategoryDto> courseCategoryDtoList = courseMapperStruct.toCourseCategoryDtoList(courseCategories);
        // 去除根节点，转换成map
        Map<String, CourseCategoryDto> courseCategoryMap = courseCategoryDtoList.stream().filter(item -> !id.equals(item.getId())).collect(Collectors.toMap((key -> key.getId()), (value -> value), (key, key2) -> key2));
        // 响应结果集合
        List<CourseCategoryDto> result = new ArrayList<>();
        // 遍历课程节点
        courseCategoryDtoList.stream().filter(item -> !id.equals(item.getId())).forEach(item -> {
            // 如果父节点与根节点相同，则添加到结果集合中
            if (id.equals(item.getParentid())) {
                result.add(item);
            }
            // 从map中获取父节点
            CourseCategoryDto parentCategoryDto = courseCategoryMap.get(item.getParentid());
            if (ObjectUtils.isNotEmpty(parentCategoryDto)) {
                // 如果父节点子节点集合为空，则新建
                if (CollectionUtils.isEmpty(parentCategoryDto.getChildrenTreeNodes())) {
                    parentCategoryDto.setChildrenTreeNodes(new ArrayList<>());
                }
                // 否则直接添加
                parentCategoryDto.getChildrenTreeNodes().add(item);
            }
        });
        return result;
    }
}
